let timer = false;
let globalTime = 0; //总时间
let week = 1;
let day = 1;
let money = 500;
let everyChef=[];   //厨师对象集 
let cookers = [];   //餐厅厨师（厨具？）对象数组   
let customers = [];//顾客对象数组
let customerName = ['Alex','Steve','Rame','Hutao','Ganyu','Lichking','Illidan'];
let customerPic = ['379339-512.png','379444-512.png','379446-512.png','379448-512.png','iconfinder_Boss-3_379348.png','iconfinder_Man-16_379485.png','iconfinder_Rasta_379441.png'];
let coldIndex = [0, 1]; 
let mainIndex = [2, 6];
let drinksIndex = [7, 8];
let allDishes = []; //食物对象数组 allFood
let dishNames = ['凉拌派蒙','冷切派蒙','清蒸派蒙','酥炸派蒙','红烧派蒙','炭烤派蒙','水煮派蒙','鲜榨派蒙','派蒙奶茶'];
let nowEating = [0, 0, 0, 0]; //用餐 
let waiting = [];                             
let willServe = [];                            
let initialPatience = [150, 250, 200, 200, 50];  
let initialEatSpeed = [100, 200, 150];        
let initialCookSpeed = [50, 150, 40];       
let i,j;
let fryNum,needth;  
let checkDishes = [];
let alertBox1Clicked = true;
let dishCost = 0;
let fireMoney = 0;
//绑定html
let blackShadow = document.querySelector('.blackShadow');//全局阴影
let gameBegin = document.querySelector('.gameBegin');//开始游戏框
let play = document.querySelector('.gameBegin .play .inline-Box');//开始游戏按钮
let dateWeek = document.getElementById("dateWeek");//当前week
let dateDay= document.getElementById("dateDay");//当前天数
let htmlMoney = document.getElementById("money");//当前金钱
let placate = document.querySelectorAll('.customerBox .placate'); //安抚
let checkout = document.querySelectorAll('.customerBox .checkout'); //结账
let customerBox = document.querySelectorAll('.customerBox'); //客人盒子         display:flex;
let waitCustomerBox =document.querySelectorAll('.waitCustomerBox');//等待的客人盒子 display:flex;
let inlineBoxColor = document.querySelectorAll('#allCustomerBox .customerBox .inline-Box-eating');       //客人盒子颜色
let eatingBar = [[],[],[],[]];  //客人进度条                display:flex;
let barColor = [[],[],[],[]];   //客人进度条颜色
let barDish = [[],[],[],[]];    //客人进度条P标签
let barButton = [[],[],[],[]];  //客人进度条右侧按钮        display:flex;
let barShade = [[],[],[],[]];   //客人进度条阴影
let drawLine = [[],[],[],[]];   //客人进度条横线
let waitShade = document.querySelectorAll('.waitCustomerBox .cookingBar .shade'); //等待客人进度条阴影
let customerImg = document.querySelectorAll('#allCustomerBox .customerBox .customerImg');                       //客人图片
let waitCustomerImg = document.querySelectorAll('.waitCustomerBox .customerImg');               //等待客人图片
let menu = document.querySelector('.menu');                                                     //显示菜单
let menuCustomerImg = document.querySelector('.menu .inline-Box .customerBox .customerImg');     //菜单客人图片
let menuCustomerName = document.querySelector('.menu .customerName');                           //菜单客人姓名
let totalPrice = document.querySelector('.menu .totalPrice');                                   //菜单总价格
let menuCheckbox = document.querySelectorAll('.menu .check');                          //菜单checkbox
let orderComplete = document.querySelector('.menu .orderCompleteBorder .inline-Box');   //点菜完成按钮
let orderNoeat = document.querySelector('.menu .noeat .inline-Box');                     //点菜不吃按钮
let allChefBox = document.getElementById('allChefBox');//总厨师盒子
let allChefInlineBox = document.querySelector('#allChefBox div:nth-of-type(1)');  //总厨师内部盒子
let chefBox = document.querySelectorAll('.chefBox');//厨师座位盒子          display:flex;
let chefBoxColor = document.querySelectorAll('.chefBox .inline-Box'); //厨师座位颜色
let chefShade = document.querySelectorAll('#allChefBox .chefBox .chef-shade'); //厨师阴影
let cookingBar = document.querySelectorAll('#allChefBox .chefBox .cookingBar');//厨师Bar    display:flex;
let cookingBarColor = document.querySelectorAll('#allChefBox .chefBox .cookingBar .inline-Bar1');//厨师Bar颜色
let cookingBarDish = document.querySelectorAll('#allChefBox .chefBox .cookingBar .dish');//厨师Bar文字
let cookingBarShade = document.querySelectorAll('#allChefBox .chefBox .cookingBar .shade');//厨师Bar阴影
let cookerImg = document.querySelectorAll('#allChefBox .chefBox .cookerImg');//厨师图片
let addChefButtons = document.querySelectorAll('#allChefBox .chefBox .addBox');//+按钮      display:flex
let multiplyBox = document.querySelectorAll('#allChefBox .chefBox .multiplyBox .inline-multiplyBox');//×按钮    display:flex
let addCookerImg = document.querySelectorAll('#allChefBox .chefBox .addCookerImgBox');//空白厨师头像
let plateImg = document.querySelectorAll('#allChefBox .chefBox .plateImg');//厨师餐盘图片
let buyChef = document.querySelector('.buyChef')                            //招聘厨师
let confirmBuyChef = document.querySelector('.buyChef .confirmRecruitment .inline-Box');//确认招聘按钮
let rejectBuyChef = document.querySelector('.buyChef .noRecruitment .inline-Box');//拒绝招聘按钮
let fryMoneyAlert = document.querySelector('.fryChef .moneyAlert');//警告解雇金钱不足按钮
let fryChef = document.querySelector('.fryChef');                   //解雇厨师
let confirmFry = document.querySelector('.fryChef .confirmFry .inline-Box');//确认解雇按钮
let noFry = document.querySelector('.fryChef .noFry .inline-Box');//取消解雇按钮
let moneyAlert = document.querySelector('.fryChef .moneyAlert'); //解雇失败弹窗
let moneyAlertp = document.querySelector('.fryChef .moneyAlert p');//解雇失败弹窗文字
let needAlert = document.querySelector('.needAlert');//粉色弹窗
let needAlertp = document.querySelector('.needAlert p');//粉色弹窗文字
let finishAlert = document.querySelector('.finishAlert');//绿色弹窗1
let finishAlertp1 = document.querySelector('.finishAlert p:nth-of-type(1)');//绿色弹窗文字1
let finishAlertp2 = document.querySelector('.finishAlert p:nth-of-type(2)');//绿色弹窗文字2
let chefAlert = document.querySelector('.chefAlert');//绿色弹窗2提醒招聘厨师成功
let chefAlertp = document.querySelector('.chefAlert p');//绿色弹窗文字
let chefNumber = document.getElementById('chefNumber');//现有厨师数量
for(i=1;i<=4;i++)
{
    eatingBar[i-1] = document.querySelectorAll('#customerBox'+i+' .eatingBar');

    barColor[i-1] = document.querySelectorAll('#customerBox'+i+' .inline-Bar1');
    barDish[i-1] = document.querySelectorAll('#customerBox'+i+' .dish');
    barButton[i-1] = document.querySelectorAll('#customerBox'+i+' .eatingBar button');
    barShade[i-1] = document.querySelectorAll('#customerBox'+i+' .eatingBar .shade');
    drawLine[i-1] = document.querySelectorAll('#customerBox'+i+' .eatingBar .drawLine');
}
play.addEventListener('click',init);

//顾客对象     
class Customers {
    constructor(name, headSculpture) {
        this.name = name;
        this.headSculpture = headSculpture;
        this.todayAte = false;
        this.waitFood = [0,0,0]; 
        this.Alltariff = 0;     //总价
        this.patience = [0,0,0]; //等菜
        this.queuePatience = 0; //队列
        this.payPatience = 0;   //结账
        this.eatingSpeed = 0;   
        this.dishesEating = [0, 0, 0];  //现在吃
        this.dishesAte = [0, 0, 0];     //吃完的
        this.missDishes = [0, 0, 0];    //没吃上的
        this.dishesWorking = [0, 0, 0]; //没做完的
        this.state = 'free';    
    }               
}
//菜对象       
class Dishes {
    constructor(type, name, cost, sell) {
        this.type = type;
        this.name = name;
        this.cost = cost;
        this.sell = sell;
        this.state = [ 0 , 0 , 0 , 0 , 0 , 0 ]; 
        this.cookingTime = [0, 0, 0, 0, 0, 0]; 
        this.belongChef = [0, 0, 0, 0, 0, 0]; //做菜的厨师
        this.freshTime = [0, 0, 0, 0, 0, 0]; //保质期
    }
}
//厨师对象 
class Chefs {
    constructor() {
        this.wages = 150;
        this.cookingDish = 0;       
        this.totalWorkTime= 0;
        this.weekWorkingTime = 0;
        this.state = "idle";        
        this.numOfDishes = 'none'; // 做哪个
    }
}
//初始化
function init() {
    setUpChef();
    setUpCustomer();
    setUpDishes();
    buyChefs();
    addListener();
    gameBegin.style.display = 'none';
    blackShadow.style.display = 'none';
    chefBox[0].style.display = 'flex';
    chefBox[1].style.display = 'flex';
    chefShade[1].setAttribute('class','addChef-shade');
    addCookerImg[1].style.display = 'initial';
    addChefButtons[1].style.display = 'initial';
    cookerImg[0].style.display = 'initial';
    timer = true;
    return true;
}
//找厨师
function buyChefs() {
    if(cookers.length >= 6)
    {
        return false;
    }
    cookers.push(everyChef[0]);
    everyChef.shift();
    return true;
}
//初始化厨师
function setUpChef () {                
    let i = 0;
    for (; i < 6; i++) 
    {
        everyChef.push(new Chefs());
    }
}
//初始化顾客
function setUpCustomer() {  
    let i = 0;
    for (; i < customerName.length; i++) 
    {
        customers.push(new Customers(customerName[i],i));
    }
}
//初始化吃的
function setUpDishes() {           
    let i = 0;
    allDishes.push(new Dishes(0, dishNames[0], 5, 12));
    allDishes.push(new Dishes(0, dishNames[1], 7, 15));
    allDishes.push(new Dishes(1, dishNames[2], 8, 16));
    allDishes.push(new Dishes(1, dishNames[3], 9, 18));
    allDishes.push(new Dishes(1, dishNames[4], 6, 12));
    allDishes.push(new Dishes(1, dishNames[5], 8, 17));
    allDishes.push(new Dishes(1, dishNames[6], 5, 10));
    allDishes.push(new Dishes(2, dishNames[7], 3, 10));
    allDishes.push(new Dishes(2, dishNames[8], 3, 10));
}
//由值求位置
function searchIndex(arr, num) {
    let i = 0;
    for (; i < arr.length; i++)
    {
        if (arr[i] === num)
        {
            return i;
        }
    }
    return false;
} 
//  每周一次
function dealWeek() {
    let i = 0
    for (; i < cookers.length; i++)
    {
        money - Math.ceil(cookers[i].weekWorkingTime / 7 * cookers[i].wages);
    }
    //厨师工作时间
    for (i = 0; i < cookers.length; i++) 
    {
        cookers[i].totalWorkTime += cookers[i].weekWorkingTime;
        cookers[i].weekWorkingTime = 0;
    }
    return true;
}   
//每天一次
function dealDaily(){   
    let i = 0;
    for (; i < cookers.length; i++)
    {
        cookers[i].weekWorkingTime++;
    }
    for (i = 0; i < nowEating.length; i++)
    {
        if (nowEating[i] && nowEating[i].state === 'anger')
        {
            appease(i); 
            nowEating[i] = 0;                               
        }
    }
    for (i = 0; i < nowEating.length; i++) 
    {
        if (nowEating[i] && nowEating[i].state === 'settleAccounts') 
        {
            settleAccounts(i);
            nowEating[i] = 0;
        }
    }
    for (i = 0; i < customers.length; i++)
    {
        customers[i].todayAte = false;
    }
}
//把厨师炒了
function  fryChefs(i) {
    if (cookers.length === 1)
    {
        return false;
    }
    else if(money < (Math.ceil(cookers[i].weekWorkingTime / 7 * cookers[i].wages) + cookers[i].wages)) 
    {
        return false;
    }
    fireMoney = (Math.ceil(cookers[i].weekWorkingTime / 7 * cookers[i].wages) + cookers[i].wages);
    money   -= fireMoney;
    cookers[i].weekWorkingTime = 0;
    cookers[i].totalWorkTime = 0;
    everyChef.push(cookers[i]);
    cookers.splice(i, 1);
    return true;
}
//厨师干活
function startCooking() {  
    let impatient  = 1000;
    let impatientNum = 'none';
    let idleCooking = false;
    let idleNum = false;
    let i = 0;
    let j = 0;
    for (; i < cookers.length; i++) 
    {
        if (cookers[i].state === 'idle') 
        {
            idleCooking = true;
            idleNum = i;
            break;
        }
    }
    if (!idleCooking)
    {
        return false;
    }
    for (i = 0; i < nowEating.length; i++) 
    {
        if (nowEating[i]) 
        {
            for (j = 0; j < 3; j++) 
            {
                if (!nowEating[i].waitFood[j])
                {
                    continue;
                }
                if (nowEating[i].state === 'seat') 
                {
                    let place = searchIndex(nowEating[i].dishesAte, nowEating[i].waitFood[j])
                    if (typeof(place) !== 'number') 
                    {
                        place = searchIndex(nowEating[i].dishesEating, nowEating[i].waitFood[j])
                            
                        if (typeof(place) !== 'number') 
                        {
                            place = searchIndex(nowEating[i].missDishes, nowEating[i].waitFood[j])
                                
                            if (typeof(place) !== 'number') 
                            {
                                place = searchIndex(nowEating[i].dishesWorking, nowEating[i].waitFood[j])
                                    
                                if (typeof(place) !== 'number') 
                                {
                                    if (nowEating[i].patience[j] < impatient) 
                                    {
                                        impatient = nowEating[i].patience[j];
                                        impatientNum = [i, j];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (impatientNum === 'none')
    {
        return false;
    }
    i = impatientNum[0];
    j = impatientNum[1];
    money -= nowEating[i].waitFood[j].cost;
    cookers[idleNum].state = 'working';
    cookers[idleNum].cookingDish = nowEating[i].waitFood[j];

    let x = searchIndex(nowEating[i].waitFood[j].state, 0);
    cookers[idleNum].numOfDishes = x;

    let y = searchIndex(nowEating[i].dishesWorking, 0);
    nowEating[i].dishesWorking[y] = nowEating[i].waitFood[j];
    nowEating[i].waitFood[j].state[x] = 'cooking';
    nowEating[i].waitFood[j].belongChef[x] = cookers[idleNum];

    let numClass = nowEating[i].waitFood[j].type;
    nowEating[i].waitFood[j].cookingTime[x] = initialCookSpeed[numClass];
    return true;
}
//菜好了
function dishesFinish() {
    let i = 0;
    let j = 0;
    for (; i < cookers.length; i++) 
    {
        if (cookers[i].state === 'working') 
        {
            j = cookers[i].numOfDishes;

            cookers[i].cookingDish.cookingTime[j]--;
            if (cookers[i].cookingDish.cookingTime[j] <= 0) 
            {
                cookers[i].cookingDish.state[j] = 'finish';

                cookers[i].state = 'complete';

                cookers[i].cookingDish.freshTime[j] = 100;

                willServe.push(cookers[i].cookingDish);
            }
        }
    }
}   
//扔掉没人吃的
function dishesCancel() {
    let i = 0;
    let j = 0;
    let k = 0;
    for (; i < allDishes.length; i++) 
    {
        for (j = 0; j < allDishes[i].state.length; j++)
        {
            if (allDishes[i].state[j] === 'finish') 
            {
                allDishes[i].freshTime[j]--;
                if (allDishes[i].freshTime[0] <= 0) 
                {
                    let num = searchIndex(willServe, allDishes[i]);
                    if (typeof(num) !== 'number')
                    {
                        continue;
                    }
                    willServe[num].belongChef[0].state = 'idle';
                    willServe[num].belongChef[0].cookingDish = 0;
                    willServe[num].belongChef[0].numOfDishes = 'none';

                    willServe[num].state.shift();
                    willServe[num].cookingTime.shift();
                    willServe[num].belongChef.shift();
                    willServe[num].freshTime.shift();

                    willServe[num].state[5] = 0;
                    willServe[num].cookingTime[5] = 0;
                    willServe[num].belongChef[5] = 0;
                    willServe[num].freshTime[5] = 0;

                    for (k = 0; willServe[num].belongChef[k]; k++) 
                    {
                        willServe[num].belongChef[k].numOfDishes--;
                    }
                    //willServe删除
                    willServe.splice(num, 1);
                }
            }
        }
    }
}
//找座
function  findSeat(i) {             
    nowEating[i] = waiting[0];
    waiting.shift();
    nowEating[i].state = 'seat'; 
    nowEating[i].queuePatience = initialPatience[3];
    nowEating[i].dishesWorking = [0,0,0];
    nowEating[i].waitFood = [0,0,0];
    nowEating[i].missDishes = [0,0,0];
    nowEating[i].dishesAte = [0,0,0];
    nowEating[i].dishesEating = [0,0,0];
    return true;
}
//来餐厅
function customerComing() {         
    if ((Math.random() * 10) > 9)
    return false;

    let i = Math.floor(Math.random()*Math.floor(customers.length));
    if (customers[i].todayAte)
    {
        return false;
    } 
    if (customers[i].state !== 'free')
        return false;
    customers[i].todayAte = true;
    if (waiting.length > 5)
        return false;
    customers[i].state = 'wait';
    customers[i].queuePatience = initialPatience[3];
    waiting.push(customers[i]);
    return true;
}
//不想排队了
function leftFormation() {
    let i = 0; 
    for (; i < waiting.length; i++) 
    {
        waiting[i].queuePatience--;
        if (waiting[i].state === 'wait' && waiting[i].queuePatience <= 0) 
        {
            waiting[i].state = 'free';
            waiting[i].queuePatience = initialPatience[3];
            waiting.splice(i, 1);
            return true;
        }
    }
    return false;
}
//清空顾客
function f5Customer(customer) {
    customer.waitFood = [0,0,0];
    customer.dishesEating = [0,0,0];
    customer.dishesAte = [0,0,0];
    customer.missDishes = [0,0,0];
    customer.dishesWorking = [0,0,0];
    customer.patience = [0,0,0];
    customer.queuePatience = initialPatience[3];
    customer.payPatience = initialPatience[4];
    customer.Alltariff = 0;
    customer.state = 'free';    
    customer.eatingSpeed = 0;
}
//错过的菜                                                      
function missDishes() {
    let i = 0;
    let j = 0;
    for (; i < nowEating.length; i++) 
    {
        if (nowEating[i]) 
        {
            for (j = 0; nowEating[i].waitFood[j]; j++) 
            {
                let num = searchIndex(nowEating[i].dishesAte, nowEating[i].waitFood[j]);
                if (typeof(num) !== 'number') 
                {
                    num = searchIndex(nowEating[i].dishesEating, nowEating[i].waitFood[j])

                    if (typeof(num) !== 'number') 
                    {
                        num = searchIndex(nowEating[i].missDishes, nowEating[i].waitFood[j])
    
                        if (typeof(num) !== 'number') 
                        {
                            nowEating[i].patience[j]--;
                            if (nowEating[i].patience[j] <= 0) 
                            {
                                nowEating[i].Alltariff -= nowEating[i].waitFood[j].sell;
                                let missNumber = searchIndex(nowEating[i].missDishes, 0);
                                nowEating[i].missDishes[missNumber] = nowEating[i].waitFood[j];
                            }
                        }
                    }
                }
            }
        }
    }
}
//不耐烦了
function impatience() {
    let i = 0;
    for (; i < nowEating.length; i++) 
    {
        if (nowEating[i]) 
        {
            let num = searchIndex(nowEating[i].waitFood, 0);
            let total = 0;
            let j=0;
            if (typeof(num) !== 'number')
            {
                num = 3;
            }
            for (j = 0; j < num; j++) 
            {
                let targetNum = searchIndex(nowEating[i].missDishes, nowEating[i].waitFood[j]);
                if (typeof(targetNum) === 'number')
                    total++;
            }
            if (total === num) 
            {
                nowEating[i].state = 'anger';
            }
        }

    }
}
//顾客吃完
function eatComplete() {
    let i = 0;
    for (; i < nowEating.length; i++) 
    {
        if (nowEating[i]) 
        {
            if (nowEating[i].dishesEating[0])
            {
                nowEating[i].state = 'eating';
            }
            if (nowEating[i].state === 'eating') 
            {
                nowEating[i].eatingSpeed--;

                if (nowEating[i].eatingSpeed <= 0) 
                {
                    let finishEatNum = searchIndex(nowEating[i].dishesAte, 0);
                    nowEating[i].dishesAte[finishEatNum] = nowEating[i].dishesEating[0];

                    nowEating[i].dishesEating.shift();
                    nowEating[i].dishesEating.push(0);

                    if (nowEating[i].dishesEating[0]) 
                    {
                        let indexNum = nowEating[i].dishesEating[0].type;
                        nowEating[i].eatingSpeed = initialEatSpeed[indexNum];
                    }
                }
            }
        }
    }
}
//顾客结账
function settleAccounts(i) {                            
    if(nowEating[i].state === 'settleAccounts') 
    {
        money += nowEating[i].Alltariff;
        needAlertp.textContent = nowEating[i].name +'完成用餐，收获$'+nowEating[i].Alltariff;
        needAlert.style['background-color'] = 'rgb(128, 245, 99)';
        needAlert.style.display = 'flex';
        f5Customer(nowEating[i]);
        nowEating[i] = 0;
        return true;
    }
}
//安抚心心
function appease(i) {           
    if (nowEating[i].state === 'anger') 
    {

        f5Customer(nowEating[i]);
        needAlertp.textContent = nowEating[i].name +'失望而归，别再让客人挨饿了';
        needAlert.style['background-color'] = 'rgb(240, 95, 191)';
        needAlert.style.display = 'flex';

        nowEating[i] = 0;

        return true;
    }
    return false;
}
//上菜
function serve(i, j) {             
    let k = 0;
    let num = searchIndex(willServe, nowEating[i].waitFood[j]);
    willServe[num].belongChef[0].state = 'idle';
    willServe[num].belongChef[0].cookingDish = 0;
    willServe[num].belongChef[0].numOfDishes = 'none';
    willServe[num].state.shift();
    willServe[num].cookingTime.shift();
    willServe[num].belongChef.shift();
    willServe[num].freshTime.shift();
    willServe[num].state[5] = 0;
    willServe[num].cookingTime[5] = 0;
    willServe[num].belongChef[5] = 0;
    willServe[num].freshTime[5] = 0;
    for (; willServe[num].belongChef[k]; k++) 
    {
        willServe[num].belongChef[k].numOfDishes--;
    }
    let position = searchIndex(nowEating[i].dishesEating, 0);
    nowEating[i].dishesEating[position] = willServe[num];
    if (!position) 
    {
        let findtype = nowEating[i].dishesEating[0].type;
        nowEating[i].eatingSpeed = initialEatSpeed[findtype];
    }
    willServe.splice(num, 1);
    return true;
}
//顾客现在状态设置
function setupState (){
    let i = 0;
    for (; i < nowEating.length; i++) 
    {
        let eatenNumber = 0;
        let missNumber = 0;
        let successFound = false;
        let settleCheck = false;
        if (nowEating[i]) 
        {
            let condition = searchIndex(nowEating[i].dishesEating, 0);
            if (typeof(condition) === 'number' && condition === 0) 
            {
                let j = 0;
                for (; nowEating[i].waitFood[j]; j++) 
                {

                    successFound = searchIndex(nowEating[i].dishesAte, nowEating[i].waitFood[j]);

                    if (typeof(successFound) === 'number') 
                    {
                        eatenNumber++;
                        continue;
                    }

                    successFound = searchIndex(nowEating[i].missDishes, nowEating[i].waitFood[j]);

                    if (typeof(successFound) === 'number') 
                    {
                        missNumber++;
                        continue;
                    }
                    nowEating[i].state = 'seat';
                    settleCheck = true;
                    break;
                }
                if (settleCheck)
                {
                    continue;
                }
                if (eatenNumber && eatenNumber + missNumber === j)
                {
                    nowEating[i].state = 'settleAccounts';
                }
                
            }
        }
    }
}    
//添加监听
function addListener() {
    let i = 0;
    for (; i < 6; i++) //+
    {
        addChefButtons[i].addEventListener("click",(event) => {
            timer = false;
            buyChef.style.display = 'flex';
            blackShadow.style.display = 'initial';
        });
    }
    //×添加监听
    for (i = 0; i < 6; i++) 
    {
        multiplyBox[i].addEventListener("click",(event) => {
            let target = event.target;
            timer = false;
            fryChef.style.display = 'flex';
            fryNum = target.dataset.index-1; 
            blackShadow.style.display = 'initial';
        });
    }
    //确认招聘
    confirmBuyChef.addEventListener("click", (event) => {

        i = cookers.length;
        buyChefs();
        if(i === 2)
        {
            allChefBox.setAttribute('class','big-allChefBox');
            allChefInlineBox.setAttribute('class','inline-Box');
        }
        addChefButtons[i].style.display = 'none';
        addCookerImg[i].style.display = 'none';
        cookerImg[i].style.display = 'initial';
        chefShade[i].setAttribute('class','chef-shade');
        multiplyBox[i].style.display = 'flex';
        buyChef.style.display = 'none';
        timer=true;

        if (i === 5)
        {
            blackShadow.style.display = 'none';
            return true;
        }
            
        chefBox[i+1].style.display = 'flex';
        chefShade[i+1].setAttribute('class','addChef-shade');
        addCookerImg[i+1].style.display = 'flex';
        addChefButtons[i+1].style.display = 'initial';
        blackShadow.style.display = 'none';
        i++;
        chefAlertp.textContent = '招聘厨师成功，您已有'+i+'名厨师';
        chefAlert.style.display = 'flex';
        return true;
    });
    //取消
    rejectBuyChef.addEventListener("click", (event) => {
        buyChef.style.display = 'none';
        blackShadow.style.display = 'none';
        timer=true;
        return true;
    });
    //确认解雇
    confirmFry.addEventListener("click", (event) => {

        let loc = fryNum; 
        let num = cookers.length; 
    if (!fryChefs(loc)) 
    {
        moneyAlert.style.display = 'flex';
        if (cookers.length === 1) 
        {
            moneyAlertp.textContent='至少有一个厨师';
            return false;
        } else {
            moneyAlertp.textContent='你的金额已经不足支付解约金';
            return false;
        }
    }
    let k,sum;
    if(num < 6)
    {
        sum=num;
    }else {
        sum=num-1;
    }
    for (k = loc; k < sum; k++)
    {
        chefBoxColor[k].setAttribute('class',chefBoxColor[k+1].getAttribute('class'));
        chefShade[k].setAttribute('class',chefShade[k+1].getAttribute('class'));  
        let newCookingBar = document.defaultView.getComputedStyle(cookingBar[k+1], null);
        let newCookingBarShade = document.defaultView.getComputedStyle(cookingBarShade[k+1], null);

        cookingBar[k].style.display = newCookingBar.display;
        if( newCookingBar.display !== 'none')
        {
            cookingBarColor[k].setAttribute('class',cookingBarColor[k+1].getAttribute('class'));
            cookingBarShade[k].style.width = newCookingBarShade.width;
            cookingBarDish[k].textContent = cookingBarDish[k+1].textContent;
        }
        let newAddCookerImg = document.defaultView.getComputedStyle(addCookerImg[k+1], null);
        if(newAddCookerImg.display !== 'none')
        {
            addCookerImg[k].style.display = 'initial';
            cookerImg[k].style.display = 'none';
            addChefButtons[k].style.display = 'flex';
        }

        let newPlateImg = document.defaultView.getComputedStyle(plateImg[k+1], null);
        plateImg[k].style.display = newPlateImg.display;

        let newMultiplyBox = document.defaultView.getComputedStyle(multiplyBox[k+1], null);
        multiplyBox[k].style.display = newMultiplyBox.display;
    }
    if( sum === num ) 
    {
        chefBox[k].style.display = 'none';
    }else 
    {
        sum=num-1;
        chefBoxColor[k].setAttribute('class','inline-Box');
        chefShade[k].setAttribute('class','addChef-shade');
        cookingBar[k].style.display = 'none';
        addCookerImg[k].style.display = 'initial';
        cookerImg[k].style.display = 'none';
        addChefButtons[k].style.display = 'flex';
        plateImg[k].style.display = 'none';
        multiplyBox[k].style.display = 'none';
    }
    fryChef.style.display = 'none';
    blackShadow.style.display = 'none'; 
    chefAlertp.textContent = '解约厨师成功，解约支出￥'+fireMoney;
    chefAlert.style.display = 'flex';

    timer = true;
    return true;
    });
    //取消
    noFry.addEventListener("click", (event) => {

    fryChef.style.display = 'none';
    blackShadow.style.display = 'none';
    timer = true;

    return true;
    });
    //排队监听
    for (i = 0; i < 6; i++) 
    {
        waitCustomerBox[i].addEventListener('click', (event) => {
        let num = 0;
        for (; num  < nowEating.length; num ++) 
        {
            if (nowEating[num] === 0)
                break;
        }
        if ( num  === 4)
            return false;
        findSeat( num );

        timer = false;
        needth = num;
        menu.style.display = 'flex';
        blackShadow.style.display = 'initial';

        dishCost = 0;
        });
    }
    //确认点餐
    orderComplete.addEventListener('click', (event) => {
        
        for (let i = 0; i < menuCheckbox.length; i++) 
        {
            menuCheckbox[i].checked = false;
        }

        menu.style.display = 'none';

        for ( i = 0; checkDishes.length; i++) 
        {
            nowEating[needth].waitFood[i] = checkDishes[0];
            nowEating[needth].Alltariff += nowEating[needth].waitFood[i].sell;
            checkDishes.shift();
        }

        for ( i = 0; i < 3; i++) 
        {
            if (!nowEating[needth].waitFood[i])
                break;
            let number = nowEating[needth].waitFood[i].type;
            nowEating[needth].patience[i] = initialPatience[number];
        }
        blackShadow.style.display = 'none';

        finishAlert.style.display = 'flex';

        finishAlertp1.textContent = menuCustomerName.innerHTML+'完成点餐，等候用餐';
        
        timer = true;

    });
    //取消
    orderNoeat.addEventListener('click', (event) => {
        for ( i = 0; i < menuCheckbox.length; i++)
        {
            menuCheckbox[i].checked = false;
        }
        menu.style.display = 'none';
        nowEating[needth].state = 'free';
        nowEating[needth] = 0;
        blackShadow.style.display = 'none';
        timer = true;
    });
    //结账                                   
    for (i = 0; i < 4; i++) 
    {
        checkout[i].addEventListener('click', (event) => {
        let target = event.target;
        let num = target.dataset.index-1;
        settleAccounts(num);
        customerImg[num].style.display = 'none';
        checkout[num].style.display = 'none';
        var j = 0;
        for (; j < 3; j++) 
        {
            eatingBar[num][j].style.display = 'none';
            drawLine[num][j].style.display = 'none';
        }
        inlineBoxColor[num].setAttribute('class','inline-Box-eating');
        return true;
        })
    }
    //安抚                                   
    for (i = 0; i < 4; i++) 
    {
        placate[i].addEventListener('click', (event) => {

            let target = event.target;
            let num = target.dataset.index-1;
            appease(num);
            customerImg[num].style.display = 'none';
            placate[num].style.display = 'none';
            for ( j = 0; j < 3; j++) {
                eatingBar[num][j].style.display = 'none';
                drawLine[num][j].style.display = 'none';
            }
            inlineBoxColor[num].setAttribute('class','inline-Box-eating');

            return true;
        })
    }
    //上餐
    for ( i = 0; i < 4; i++) 
    {
        for ( j = 0; j < 3; j++) 
        {
            barButton[i][j].addEventListener('click', (event) => {

                let target = event.target;
                let numj = target.dataset.index-1;
                let numi = target.dataset.allindex-1;
                serve(numi,numj);
                barButton[numi][numj].style.display = 'none';
                barColor[numi][numj].setAttribute('class','inline-Bar3');
                inlineBoxColor[numj].setAttribute('class','inline-Box-eating2');

                return true;
            });
        }
    }
    //各弹框
    needAlert.addEventListener('click',(event) =>{
        alertBox1Clicked = false;
        needAlert.style.display = 'none';
    });
    finishAlert.addEventListener('click',(event) =>{
        finishAlert.style.display = 'none';
    });
    moneyAlert.addEventListener('click',(event) =>{
        moneyAlert.style.display = 'none';
    });
    chefAlert.addEventListener('click',(event) =>{
        chefAlert.style.display = 'none';
    });
}
//f5
function f5Formation(){  
    let i = 0;
    for (; i < waiting.length; i++) 
    {
        waitCustomerBox[i].style.display = 'flex';
        waitCustomerImg[i].src = 'images/'+customerPic[waiting[i].headSculpture];
        waitCustomerImg[i].style.display = 'initial';

        waitShade[i].style.width = 59.8 - 59.8 / initialPatience[3] * waiting[i].queuePatience + 'px';

    }
    for (; i < 6; i++) 
    {
        waitCustomerBox[i].style.display = 'none';
    }
}
function getFirstFormation() {
    if(waiting.length)
    {
        menuCustomerImg.src = 'images/'+customerPic[waiting[0].headSculpture];
        menuCustomerName.textContent = waiting[0].name;
    }
}
function alertBox() {
    let i=0;
    let num = 0;
    for(; i < nowEating.length ; i++)
    {
        if(nowEating[i] === 0 )
        {
            num++;
        }
    }
    if(num === 4 && alertBox1Clicked)
    {
        needAlert.style['background-color'] = 'rgb(240, 95, 191)';
        needAlertp.textContent = '餐厅目前有空位，赶紧点击等位客人头像让客人入座点餐吧';
        needAlert.style.display = 'flex';
    }
}
function f5Seats() {
    let i = 0
    let j = 0;
    for ( ; i < 4; i++) 
    {
        if (nowEating[i]) 
        {
            customerImg[i].src = 'images/'+customerPic[nowEating[i].headSculpture];
            customerImg[i].style.display = 'initial';
            switch (nowEating[i].state) 
            {
                case 'seat':
                { 
                    inlineBoxColor[i].setAttribute('class','inline-Box-eating1'); 
                    break; 
                }
                case 'eating':
                { 
                    inlineBoxColor[i].setAttribute('class','inline-Box-eating2'); 
                    break; 
                }
                case 'settleAccounts':
                { 
                    inlineBoxColor[i].setAttribute('class','inline-Box-eating3'); 
                    break; 
                }
                case 'anger':
                { 
                    inlineBoxColor[i].setAttribute('class','inline-Box-eating4'); 
                    break; 
                }
                default:
                    break;
            }
            //设置bar
            for (j=0; j < 3; j++) 
            {
                //是餐食
                if (typeof(nowEating[i].waitFood[j]) === 'object') 
                {
                    //设置bar上的文字
                    eatingBar[i][j].style.display = 'flex';
                    barDish[i][j].textContent = nowEating[i].waitFood[j].name;

                    if(typeof(searchIndex(nowEating[i].dishesEating, nowEating[i].waitFood[j])) === 'number') //dishesEating
                    {
                        barColor[i][j].setAttribute('class','inline-Bar3');

                        //shadow长度控制
                        barShade[i][j].style.width = 59.8 - 59.8 / initialEatSpeed[nowEating[i].waitFood[j].type] * nowEating[i].eatingSpeed + 'px';

                        if (searchIndex(nowEating[i].dishesEating, nowEating[i].waitFood[j]))
                        {
                            barShade[i][j].style.width = '0px';
                        }
                        continue;
                    }
                    else if (typeof(searchIndex(nowEating[i].dishesAte, nowEating[i].waitFood[j])) === 'number')//dishesAte
                    {
                        barShade[i][j].style.width = '0px';
                        barColor[i][j].setAttribute('class','inline-Bar2');
                        continue;
                    }
                    else if (typeof(searchIndex(nowEating[i].missDishes, nowEating[i].waitFood[j])) === 'number') //missDishes
                    {
                        barColor[i][j].setAttribute('class','inline-Bar4');

                        barShade[i][j].style.width = '0px';
                        drawLine[i][j].style.display = 'initial';
                        barButton[i][j].style.display = 'none';
                        continue;
                    }
                    else   //等待中
                    {
                        barColor[i][j].setAttribute('class','inline-Bar1');
                    
                        //如果菜做好了，显示上菜
                        if (nowEating[i].waitFood[j].state[0] === 'finish')
                        {
                            barButton[i][j].style.display = 'flex';
                        }
                        //如果没做好，不显示
                        if (nowEating[i].waitFood[j].state[0] !== 'finish')
                        {
                            barButton[i][j].style.display = 'none';
                        }
                        //shadow长度控制
                        barShade[i][j].style.width = 59.8 - 59.8 / initialPatience[nowEating[i].waitFood[j].type] * nowEating[i].patience[j] + 'px';
                        continue;
                    }
                }
            }
            //设置结账和安抚
            if (nowEating[i].state === 'settleAccounts')
            {
                checkout[i].style.display = 'flex';
            }
            if (nowEating[i].state === 'anger') 
            {
                placate[i].style.display = 'flex';
            }
        }
        //座位上没人
        else 
        {
            //清空头像
            customerImg[i].style.display = 'none';
            //清空座位颜色
            inlineBoxColor[i].setAttribute('class','inline-Box-eating');
            //清空bar
            for (j = 0; j < 3; j++) 
            {
                barShade[i][j].style.width = '0px';
                eatingBar[i][j].style.display = 'none';
                drawLine[i][j].style.display = 'none';
                barColor[i][j].setAttribute('class','inline-Bar1');
            }
            //清空结账和安抚
            placate[i].style.display = 'none';
            checkout[i].style.display = 'none';
        }
    }
}
function f5Chefs() {

    if (cookers.length < 3) 
    {
        allChefBox.setAttribute('class','small-allChefBox');
        allChefInlineBox.setAttribute('class','small-inline-Box');
    }
    let i = 0;
    for ( ; i < cookers.length; i++) 
    {
        
        if (cookers[i].state === 'idle')  
        {
            
            chefBoxColor[i].setAttribute('class','inline-Box');
            cookingBar[i].style.display = 'none';
            cookingBarShade[i].style.width = '0px';
            plateImg[i].style.display = 'none';

            addChefButtons[i].style.display = 'none';
            addCookerImg[i].style.display = 'none';

            chefShade[i].setAttribute('class','chef-shade');

            if (cookers.length !== 1)
            {
                multiplyBox[i].style.display = 'flex';
            }
            else
            {
                multiplyBox[i].style.display = 'none';
            }
        }
        else if (cookers[i].state === 'working')    
        {
  
            chefBoxColor[i].setAttribute('class','inline-Box-cooking1');

            cookingBar[i].style.display = 'flex';
            cookingBarColor[i].setAttribute('class','inline-Bar1');
            let j = cookers[i].numOfDishes;

            cookingBarShade[i].style.width = 59.8 - 59.8 / initialCookSpeed[cookers[i].cookingDish.type] * cookers[i].cookingDish.cookingTime[j] + 'px';

            cookingBarDish[i].textContent = cookers[i].cookingDish.name;

            plateImg[i].style.display = 'none';

            multiplyBox[i].style.display = 'none';
            addChefButtons[i].style.display = 'none';
            addCookerImg[i].style.display = 'none';
        }
        else if (cookers[i].state === 'complete')   
        {
            chefBoxColor[i].setAttribute('class','inline-Box-cooking2');
            cookingBar[i].style.display = 'flex';
            cookingBarColor[i].setAttribute('class','inline-Bar2');
            cookingBarShade[i].style.width = '100%';

            cookingBarDish[i].textContent = cookers[i].cookingDish.name;

            plateImg[i].style.display = 'initial';

            multiplyBox[i].style.display = 'none';

            addChefButtons[i].style.display = 'none';
            addCookerImg[i].style.display = 'none';
        }
    }
}
function f5Menu() { 
    let dishes = [0, 0, 0];
    let priceSum = 0;
    checkDishes = [];
    if (menuCheckbox[0].checked)
    {
        dishes[0]++;
        priceSum += 12;
        dishCost += 5;
        checkDishes.push(allDishes[0]);
    }
    if (menuCheckbox[1].checked)
    {
        dishes[0]++;
        dishCost += 7;
        priceSum += 15;
        checkDishes.push(allDishes[1]);
    }

    if (menuCheckbox[2].checked) 
    {
        dishes[1]++;
        dishCost += 8;
        priceSum += 16;
        checkDishes.push(allDishes[2]);
    }
    if (menuCheckbox[3].checked) 
    {
        dishes[1]++;
        dishCost += 9;
        priceSum += 18;
        checkDishes.push(allDishes[3]);
    }
    if (menuCheckbox[4].checked) 
    {
        dishes[1]++;
        dishCost += 6;
        priceSum += 12;
        checkDishes.push(allDishes[4]);
    }
    if (menuCheckbox[5].checked) 
    {
        dishes[1]++;
        dishCost += 8;
        priceSum += 17;
        checkDishes.push(allDishes[5]);
    }
    if (menuCheckbox[6].checked) 
    {
        dishes[1]++;
        dishCost += 5;
        priceSum += 10;
        checkDishes.push(allDishes[6]);
    }

    if (menuCheckbox[7].checked) 
    {
        dishes[2]++;
        dishCost += 3;
        priceSum += 10;
        checkDishes.push(allDishes[7]);
    }
    if (menuCheckbox[8].checked) 
    {
        dishes[2]++;
        dishCost += 3;
        priceSum += 10;
        checkDishes.push(allDishes[8]);
    }
    totalPrice.textContent = priceSum;

    if (dishes[0] > 1 || dishes[1] !== 1 || dishes[2] > 1 ) 
    {
        
        orderComplete.style['background-color'] = 'gray';   
        orderComplete.style['pointer-events'] = 'none';
    } else 
    {
        orderComplete.style['background-color'] = 'rgb(255, 217, 0)';
        orderComplete.style['pointer-events'] = 'auto';
    }

}
//计时
function chronography () {    
    f5Menu();
    if (timer)
    {
        globalTime++;
        f5Formation();
        f5Seats();
        f5Chefs();
        getFirstFormation();
        alertBox();
        dateWeek.textContent = week;
        dateDay.textContent = day;
        htmlMoney.textContent = money;
        customerComing();
        leftFormation();
        startCooking();
        dishesFinish();
        missDishes();
        impatience();
        eatComplete();
        setupState();
        dishesCancel();
    }
    if (globalTime >= 240) 
    {
        day++;
        globalTime = 0;
        dealDaily();
    }
    if (day > 7) 
    {
        week++;
        day = 1;
        dealWeek();
    }
}
setInterval(chronography, 100);